#include "WifiServer.h"
#include "HttpCmdProcess.h"
#include "Protocol/ProtocolApi.h"
#include "ScreenMirrorStream.h"
#include "errorCode.h"

using namespace Protocol;

WifiServer *WifiServer::sm_wifiServer = nullptr;
mutex WifiServer::sm_instanceLock;

WifiServer *WifiServer::instance(){
	lock_guard<mutex> instance(sm_instanceLock);
	if(sm_wifiServer == nullptr){
		sm_wifiServer = new WifiServer();
	}
	return sm_wifiServer;
}


WifiServer::WifiServer(){
	RegistRPC();
}

WifiServer::~WifiServer(){

}

void WifiServer::RegistRPC(){
	RPCMethodMap method_map;
	method_map.emplace("ScanWifi", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return ScanWifi(request, inMap, response, outMap);
	}, true));

	method_map.emplace("ConnectWifi", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return ConnectWifi(request, inMap, response, outMap);
	}, true));

	method_map.emplace("OpenApService", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return openApService(request, inMap, response, outMap);
	}, true));
	IProtocolInterface::GetInstance()->RegistProtocol("Wifi", method_map);
}

bool WifiServer::ScanWifi(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outBuffer){
	if(Media::ScreenMirrorStream::instance()->getShareScreenStatus()){
		response["code"] = Media::ScreenPlaying;
		response["msg"] = "Wireless network Settings are not allowed when the device is projecting the screen.";
		return false;
	}
	Json::Value apList;
	if(!(Media::HttpCmdProcess::instance() -> scanAP(apList))){
		return false;
	}
	response["data"]["apList"] = apList["apList"];
	tracef("apList ----> %s\n", apList.toStyledString().c_str());
	return true;
}

bool WifiServer::ConnectWifi(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outBuffer){
	if(Media::ScreenMirrorStream::instance()->getShareScreenStatus()){
		response["code"] = Media::ScreenPlaying;
		response["msg"] = "Wireless network Settings are not allowed when the device is projecting the screen.";
		return false;
	}
	if(!request.isMember("Param")){
		return false;
	}
	Json::Value table = request["Param"];
	return Media::HttpCmdProcess::instance()->ConnectWifi(table);
}

bool WifiServer::openApService(const Json::Value &request, const Fdt::BufferMap::Ptr &inMap, Json::Value &response, Fdt::BufferMap::Ptr &outBuffer){
	if(Media::ScreenMirrorStream::instance()->getShareScreenStatus()){
		response["code"] = Media::ScreenPlaying;
		response["msg"] = "Wireless network Settings are not allowed when the device is projecting the screen.";
		return false;
	}
	if(!request.isMember("Param")){
		return false;
	}
	Json::Value table = request["Param"];
	return Media::HttpCmdProcess::instance() -> openApService(table);
}